const Base = require('./base.js');

module.exports = class extends Base {
	constructor(ctx) {
		super(ctx);
		this.comment = this.model('store/comment');

	}
	async indexAction() {
		if(this.isGet) {
			return this.display();
		}

		let posts = this.post();

		let where = {};
		if(!think.isEmpty(posts.userid)) {
			where.user_id = posts.userid;
		}

		const data = await this.comment.dataTable(posts, where);

		console.log(JSON.stringify(data))
		return this.json(data);
	}

	async editAction() {
		try {
			if(this.isGet) {
				let gets = this.get();

				let comment = await this.comment.findOne(gets.id);
				this.assign("comment", comment || {});
				return this.display();
			}
			if(this.isPost) {

				let posts = this.post();
				await this.comment.setOne(posts.id, {
					reply: posts.content,
					reply_time: new Date().valueOf()
				});

				return this.success("发送成功");

			}
		}
		catch(e) {
			console.log(e)
		}
	}
};